﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class WithStatement : BodiedStatement
{
    private readonly SyntaxToken _withKeywordToken;
    private Expression _expression;

    internal WithStatement(SyntaxToken withKeywordToken, SyntaxNode? parent) :
        base(SyntaxKinds.WithStatement, parent)
    {
        SetChildField(ref _withKeywordToken, withKeywordToken);
        _expression = EmptyExpression.Default;
    }


    public SyntaxToken WithKeywordToken => _withKeywordToken;

    public Expression Expression => _expression;


    public WithStatement Update(Expression expression)
    {
        SetChildField(ref _expression, Expression.IsEmptyExpression, expression);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, WithKeywordToken, Expression);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static WithStatement CreateWithStatement(SyntaxNode? parent, SyntaxToken withKeywordToken,
        Expression? expression)
    {
        Debug.Assert(withKeywordToken.IsNode(SyntaxKinds.WithKeywordToken));
        WithStatement stat = new(withKeywordToken, parent);
        if (!Expression.IsEmptyExpression(expression))
        {
            stat.Update(expression);
        }

        return stat;
    }
}